package com.example.demo.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class DataController {


    @PostMapping(value = "/login")
    public Object login(
            @RequestParam(value = "tel", required = false, defaultValue = "0") String tel,
            @RequestParam(value = "pwd", required = false, defaultValue = "0") String pwd,
            HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        if (tel.equals("admin")&&pwd.equals("123456")){
            map.put("msg","登录成功");
            map.put("code",0);
        }else{
            map.put("msg","登录失败");
            map.put("code",1);
        }
        request.getSession().setAttribute("user","admin");
        return map;
    }
    @PostMapping(value = "/cancelAccount")
    public Object cancelAccount(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        try {
            request.getSession().invalidate();
            map.put("code",0);
        }catch (Exception e){
            map.put("code",1);
            e.printStackTrace();
        }
        return map;
    }
}
